package vn.igolf.geo;


public class Line {
	public static double EPSILON = 1e-7;
	private Point aP;
	private Point bP;
	private double aL;
	private double bL;

	public double getaL() {
		return aL;
	}

	public void setaL(double aL) {
		this.aL = aL;
	}

	public double getbL() {
		return bL;
	}

	public void setbL(double bL) {
		this.bL = bL;
	}

	public Point getAP() {
		return aP;
	}

	public void setAP(Point a) {
		this.aP = a;
	}

	public Point getBP() {
		return bP;
	}

	public void setB(Point b) {
		this.bP = b;
	}

	public Line (Point a, Point b) {
		this.aP = a;
		this.bP = b;
		double aX = a.getX();
		double aY = a.getY();
		double bX = b.getX();
		double bY = b.getY();
		if(Math.abs(aX-bX)<EPSILON) Double.isInfinite(this.aL);
		else{
			this.aL = (aY-bY)/(aX-bX);
			this.bL = aY-aL*aX;
		}
	}

	public Point intersect(Line other) {
		if (other.aL == this.aL)
			return null;
		else if (Double.isInfinite(other.aL)) {
			return new Point(other.aP.getX(), this.aL * other.aP.getX()
					+ this.bL);
		} else if (Double.isInfinite(this.aL)) {
			return new Point(this.aP.getX(), other.aL * this.aP.getX()
					+ other.bL);
		} else {
			double xP = (other.bL - this.bL) / (this.aL - other.aL);
			double yP = this.aL * xP + this.bL;
			Point P = new Point(xP, yP);
			return P;
		}
	}

}
